<?php 

class MenusController extends AppController {
	var $name = 'Menus';
	var $layout = 'admin';
	var $helpers = array('Javascript');
	var $components = array('Auth', 'SimpleUpload');
	
	const UploadDir = 'files/menu';
	
	
	function beforeFilter(){
		$this->Auth->allow();
		$username = $this->Auth->user();
		$this->set('username', $username['User']['username']);
		
		//$this->FileUpload->fields['name'] = 'image_eng_passive';
	}
	
	public function index(){
		
		$menus = $this->Menu->find('all');
		$this->set('menus', $menus);
	}

	public function getTopMenus(){
		
		return $menus = $this->Menu->find('all', array('conditions' => array('parent_id'=>'-1')));
	}
	
	public function delete($id=NULL){
		if ($id) {
			if($this->Menu->delete($id)){
				$this->Session->setFlash('Menu deleted successfuly!');
				$this->redirect($this->referer());
			}
			else{
				$this->Session->setFlash('Failed! Please try again or contact to administrator.');
				$this->redirect($this->referer());
			}
			
		}
		else{
			$this->Session->setFlash('Failed! No such menu.');
			$this->redirect($this->referer());
			
		}
	}
	
	public function deleteImage($id=NULL, $field){
		if ($id) {
			if($this->Menu->findById($id)){
				$this->Menu->id = $id;
				$menu = $this->Menu->findById($id);
				$file = $menu['Menu'][$field];
				if(file_exists(WWW_ROOT.$file)){
					
					unlink(WWW_ROOT.$file);
				}
				$this->Menu->saveField($field, NULL);
				
				$this->Session->setFlash('Menu Image deleted successfuly!');
				$this->redirect($this->referer());
			}
			else{
				$this->Session->setFlash('Failed! Please try again or contact to administrator.');
				$this->redirect($this->referer());
			}
			
		}
		else{
			$this->Session->setFlash('Failed! No such menu.');
			$this->redirect($this->referer());
			
		}
	}
	
	public function add(){
		if (!empty($this->data)) {
			
			//var_dump($this->data); die();
			
			$saveable = array(
				"name_eng" => $this->data['Menu']['name_eng'],
			    "name_rus" => $this->data['Menu']['name_rus'],
			    "name_arm" => $this->data['Menu']['name_arm'],
			    "url" => ($this->data ['Menu']['url'] == -2) ? $this->data['Menu']['url_other'] : $this->data ['Menu']['url'],
			    "parent_id" => $this->data['Menu']['parent_id']
			
			);
			
			if(!$this->_checkImageSize())
				$this->redirect($this->referer());				
				
			if(!$this->_uploadFiles($saveable))
				$this->redirect($this->referer());				
				
				
			$this->Menu->create();
			if($this->Menu->save($saveable)){
				if (isset($this->params['form']['edit'])) {
					$this->Session->setFlash('Menu added successfuly!');
					$this->redirect(array('action'=>'edit', $this->Menu->id ));
				}else{
					$this->Session->setFlash('Menu ('. $this->Menu->id .') added successfuly!');
					$this->redirect(array('action'=>'index' ));
				}
			}else{
				$this->Session->setFlash('Failed to add menu!');
				$this->redirect(array('action'=>'index' ));
			}
		}
		else{
			$this->Content =& ClassRegistry::init('Content');
			$allContents = $this->Content->find('all', array('conditions' => array('is_block' => '0')));
			$contentsUrls['-1'] = '--select--';
			$contentsUrls['-2'] = '--other--';
			
			foreach ($allContents as $allContent){
				$contentsUrls[$allContent['Content']['name']] = $allContent['Content']['name']; 
			}
			$this->set('contentsUrls', $contentsUrls);
		}
	}
	
	public function edit($id){
		if (!empty($this->data)) {
			$this->Menu->id = $id;
			$saveable = array(
				"name_eng" => $this->data['Menu']['name_eng'],
			    "name_rus" => $this->data['Menu']['name_rus'],
			    "name_arm" => $this->data['Menu']['name_arm'],
			    "url" => ($this->data ['Menu']['url'] == -2) ? $this->data['Menu']['url_other'] : $this->data ['Menu']['url'],
			    "parent_id" => $this->data['Menu']['parent_id']
			
			);
			
			if(!$this->_checkImageSize())
				$this->redirect($this->referer());				
				
			if(!$this->_uploadFiles($saveable))
				$this->redirect($this->referer());				
				
			if($this->Menu->save($saveable)){
				if (isset($this->params['form']['edit'])) {
					$this->Session->setFlash('Menu edited successfuly!');
					$this->redirect(array('action'=>'edit', $this->Menu->id ));
				}else{
					$this->Session->setFlash('Menu ('. $this->Menu->id .') edited successfuly!');
					$this->redirect(array('action'=>'index' ));
				}
			}else{
				$this->Session->setFlash('Failed to add menu!');
				$this->redirect(array('action'=>'index' ));
			}
		}else{
			
			$this->Content =& ClassRegistry::init('Content');
			$allContents = $this->Content->find('all', array('conditions' => array('is_block' => '0')));
			$contentsUrls['-1'] = '--select--';
			$contentsUrls['-2'] = '--other--';
			
			foreach ($allContents as $allContent){
				$contentsUrls[$allContent['Content']['name']] = $allContent['Content']['name']; 
			}
			$this->set('contentsUrls', $contentsUrls);
			
			$this->data = $this->Menu->findById($id);
		}
	}
	
	private function _checkImageSize(){
		$error_msg = "";
		//English version
		if(!empty($this->data['Menu']['image_eng_passive']['tmp_name'])){
			list($width_eng, $height_eng, $type, $attr) = getimagesize($this->data['Menu']['image_eng_passive']['tmp_name']);
			
			if($this->data['Menu']['image_eng_active']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_eng_active']['tmp_name']);
				if($width != $width_eng || $height != $height_eng){
					$error_msg = "Active mode image size on English version does not match Passive mode";
				}
			}
			if($this->data['Menu']['image_eng_hover']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_eng_hover']['tmp_name']);
				if($width != $width_eng || $height != $height_eng){
					$error_msg = "Hover mode image size on English version does not match Passive mode";
				}
			}
		}
		
		//Russian version
		if(!empty($this->data['Menu']['image_rus_passive']['tmp_name'])){
			list($width_rus, $height_rus, $type, $attr) = getimagesize($this->data['Menu']['image_rus_passive']['tmp_name']);
			
			if($this->data['Menu']['image_rus_active']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_rus_active']['tmp_name']);
				if($width != $width_rus || $height != $height_rus){
					$error_msg = "Active mode image size on Russian version does not match Passive mode";
				}
			}
			if($this->data['Menu']['image_rus_hover']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_rus_hover']['tmp_name']);
				if($width != $width_rus || $height != $height_rus){
					$error_msg = "Hover mode image size on Russian version does not match Passive mode";
				}
			}
		}
	
		//Armenian version
		if(!empty($this->data['Menu']['image_arm_passive']['tmp_name'])){
			list($width_arm, $height_arm, $type, $attr) = getimagesize($this->data['Menu']['image_arm_passive']['tmp_name']);
			
			if($this->data['Menu']['image_arm_active']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_arm_active']['tmp_name']);
				if($width != $width_arm || $height != $height_arm){
					$error_msg = "Active mode image size on Armenian version does not match Passive mode";
				}
			}
			if($this->data['Menu']['image_arm_hover']['tmp_name']){
				list($width, $height, $type, $attr) = getimagesize($this->data['Menu']['image_arm_hover']['tmp_name']);
				if($width != $width_arm || $height != $height_arm){
					$error_msg = "Hover mode image size on Armenian version does not match Passive mode";
				}
			}
		}
	
		
		if($error_msg != ""){
			$this->Session->setFlash($error_msg);
			return false;
		}
		
		return true;
	}
	

	private function _uploadFiles(&$saveable){
		
		//English version
		if($this->data['Menu']['image_eng_passive'] && $this->data['Menu']['image_eng_passive']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_eng_passive']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_eng_passive'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_eng_active'] && $this->data['Menu']['image_eng_active']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_eng_active']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_eng_active'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_eng_hover'] && $this->data['Menu']['image_eng_hover']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_eng_hover']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_eng_hover'] = $fileOk['urls'][0]; 
			}
		}		
				
		//Russian version		
		if($this->data['Menu']['image_rus_passive'] && $this->data['Menu']['image_rus_passive']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_rus_passive']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_rus_passive'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_rus_active'] && $this->data['Menu']['image_rus_active']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_rus_active']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_rus_active'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_rus_hover'] && $this->data['Menu']['image_rus_hover']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_rus_hover']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_rus_hover'] = $fileOk['urls'][0]; 
			}
		}		
				
		//Armenian version		
		if($this->data['Menu']['image_arm_passive'] && $this->data['Menu']['image_arm_passive']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_arm_passive']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_arm_passive'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_arm_active'] && $this->data['Menu']['image_arm_active']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_arm_active']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_arm_active'] = $fileOk['urls'][0]; 
			}
		}		
		if($this->data['Menu']['image_arm_hover'] && $this->data['Menu']['image_arm_hover']['size']!=0){
			$fileOk	= $this->SimpleUpload->uploadFiles(self::UploadDir, array( $this->data['Menu']['image_arm_hover']));
			if(array_key_exists('errors', $fileOk))
			{
				$sErrorMessages = implode(',', $fileOk['errors']);
				$this->Session->setFlash( $sErrorMessages );
				return;
			}else if(array_key_exists('urls',$fileOk))
			{
				$saveable['image_arm_hover'] = $fileOk['urls'][0]; 
			}
		}		
		
		return true;
	
	
	}

	

}

?>